package com.simple.weibo.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.ZoomControls;
import com.simple.weibo.service.DownPicService;
import com.simple.weibo.view.ImageZoomView;
import com.simple.weibo.view.SimpleZoomListener;
import com.simple.weibo.view.ZoomState;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class ViewPicActivity extends Activity  implements View.OnClickListener{
    /**
     * Called when the activity is first created.
     */
    private ImageZoomView mZoomView;
    private ZoomState mZoomState;
    private Bitmap mBitmap;
    private SimpleZoomListener mZoomListener;
    private ProgressBar progressBar;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            progressBar.setVisibility(View.GONE);
            mZoomView.setImage(mBitmap);
            mZoomState = new ZoomState();
            mZoomView.setZoomState(mZoomState);
            mZoomListener = new SimpleZoomListener();
            mZoomListener.setZoomState(mZoomState);
            mZoomView.setOnTouchListener(mZoomListener);
            resetZoomState();
        }
    };

    private Button backBtn;
    private Button downBtn;

    private String url;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_pic);
        url = getIntent().getStringExtra("url");

        mZoomView = (ImageZoomView) findViewById(R.id.zoomView);
        progressBar = (ProgressBar) findViewById(R.id.progress_large);
        backBtn = (Button) findViewById(R.id.backBtn);
        downBtn = (Button) findViewById(R.id.downBtn);
        backBtn.setOnClickListener(this);
        downBtn.setOnClickListener(this);

        progressBar.setVisibility(View.VISIBLE);
        Thread thread = new Thread(new Runnable() {

            public void run() {
                try {
                    mBitmap = BitmapFactory.decodeStream(new URL(url).openStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(0);
            }
        });
        thread.start();

        ZoomControls zoomCtrl = (ZoomControls) findViewById(R.id.zoomCtrl);
        zoomCtrl.setOnZoomInClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                float z = mZoomState.getZoom() + 0.25f;
                if (z > 3) {
                    z = 3;
                }
                mZoomState.setZoom(z);
                mZoomState.notifyObservers();
            }
        });
        zoomCtrl.setOnZoomOutClickListener(new View.OnClickListener() {


            public void onClick(View v) {
                float z = mZoomState.getZoom() - 0.25f;
                if (z < 0.3) {
                    z = 0.3f;
                }

                mZoomState.setZoom(z);
                mZoomState.notifyObservers();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBitmap != null)
            mBitmap.recycle();
    }

    private void resetZoomState() {
        mZoomState.setPanX(0.5f);
        mZoomState.setPanY(0.5f);
        mZoomState.setZoom(1f);
        mZoomState.notifyObservers();
    }

    public void onClick(View v) {
          switch (v.getId()){
              case R.id.backBtn:
                  this.finish();
                  break;
              case R.id.downBtn:
                  Toast.makeText(this,url+"开始下载",Toast.LENGTH_SHORT).show();
                  Intent intent = new Intent(this, DownPicService.class);
                  intent.putExtra("url",url);
                  startService(intent);
                  break;
          }
    }
}
